package Dao;

import Entity.Pattern;
import Entity.Question;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PatternDao {
    //添加评估模板到临时文件中
    public static void StartAssess(Pattern pattern) throws IOException {
        File file = new File("files\\Tmp.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        list.add(pattern);
        om.writeValue(file,list);
    }
    //读取临时文件中的评估模板
    public static Pattern StartAssess() throws IOException {
        File file = new File("files\\Tmp.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        Pattern p = list.get(0);
        Pattern pattern = new Pattern(p.getName(),p.getType(),p.getQuestions());
        return pattern;
    }

    //添加模板到文件中
    public static void AddPattern(Pattern pattern) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        list.add(pattern);
        om.writeValue(file,list);
    }
    //将全部的模板覆盖进文件中
    public static void StorePatterns(List<Pattern> list) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        om.writeValue(file,list);
    }
    //删除某模板
    public static void deletePattern(Pattern pattern) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        for(Pattern p:list){
            if(pattern.equals(p)){
                list.remove(pattern);
                break;
            }
        }
        om.writeValue(file,list);
    }

    //返回全部的模板
    public static List<Pattern> getPatterns() throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        return list;
    }
    //返回一个模板
    public static Pattern getPattern(String name) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        for(Pattern pattern:list){
            if(pattern.getName().equals(name)) return pattern;
        }
        return null;
    }

    //根据模板名称返回对应问题的集合
    public static List<Question> byName(String name) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        for(Pattern pattern:list){
            if(pattern.getName().equals(name)) return pattern.getQuestions();
        }
        return null;
    }
    public static List<Question> UnbyName(String name) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }
        List<Question> questions = new ArrayList<>(QuestionDao.getQuestions());
        for(Pattern pattern:list){
            if(pattern.getName().equals(name)){
                for(Question question:pattern.getQuestions()){
                    for(Question q:questions){
                        if(question.equals(q)) questions.remove(q);
                    }
                }
            }
        }
        return questions;
    }


    //根据问题的名字从某模板中删除
    public static void deleteQuestion(Pattern pattern,Question question) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }

        for(Pattern p:list){
            if(p.equals(pattern)){
                List<Question> questions = p.getQuestions();
                boolean j=false;
                for(int i=0;i<questions.size();i++){
                    if(questions.get(i).getContent().equals(question.getContent())){
                        j=true;
                        questions.remove(i);
                    }
                    if(j){
                        if(i<questions.size()-1){
                            Question q = questions.get(i);
                            q.setNum(questions.get(i).getNum()+1);
                            questions.set(i,q);
                        }
                        p.setQuestions(questions);
                    }
                }
            }
        }
        om.writeValue(file,list);
    }

    //向模板中添加问题
    public static void addtoPattern(Pattern pattern,Question question) throws IOException {
        File file = new File("files\\Pattern.json");
        ObjectMapper om = new ObjectMapper();
        List<Pattern> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Pattern>>() {});
        }

        for(Pattern p:list){
            if(p.getQuestions()!=null){
                List<Question> questions = new ArrayList<>(p.getQuestions());
                if (p.equals(pattern)){
                    questions.add(question);
                    p.setQuestions(questions);
                }
            }
            else{
                List<Question> questions = new ArrayList<>();
                if (p.equals(pattern)){
                    questions.add(question);
                    p.setQuestions(questions);
                }
            }
        }
        om.writeValue(file,list);
    }

    //根据模板返回非该模板下的问题的集合
    public static List<Question> UnbyName(Pattern pattern) throws IOException {
        File file = new File("files\\Question.json");
        ObjectMapper om = new ObjectMapper();
        List<Question> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Question>>() {});
        }

        if (pattern.getQuestions()!=null){
            List<Question> questions = new ArrayList<>(pattern.getQuestions());
            for(Question q:questions){
                for(Question question:list)
                    if(question.equals(q)){
                        list.remove(question);
                        break;
                    }
            }
        }
        return list;
    }
}
